package com.car.serve.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.serve.Param.addTypeParam;
import com.car.serve.dto.ServeTypeDto;
import com.car.serve.entity.ServeInfo;
import com.car.serve.entity.ServeType;
import com.car.serve.exception.ServeException;
import com.car.serve.form.PageServeTypeForm;
import com.car.serve.mapper.ServeTypeMapper;
import com.car.serve.service.ServeTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.car.serve.util.PageDtoUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@Service
public class ServeTypeServiceImpl extends ServiceImpl<ServeTypeMapper, ServeType> implements ServeTypeService {

    @Resource
    private ServeTypeMapper serveTypeMapper;
    @Override
    public void add(addTypeParam param) throws Exception {
        //以类名为条件去查询
        QueryWrapper<ServeType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("serve_type_name",param.getInsertServeTypeName());
        ServeType serveType = serveTypeMapper.selectOne(queryWrapper);
        System.out.println(param.getInsertServeTypeName());
        if (serveType!=null){
            throw new ServeException(3000,"类别名重复");
        }
        ServeType serveType1 = BeanUtil.toBean(param, ServeType.class);
        serveType1.setServeTypeName(param.getInsertServeTypeName());
        //执行新增
        serveTypeMapper.insert(serveType1);
    }


    @Override
    public Page<ServeTypeDto> findServeTypePage(PageServeTypeForm form) {
        Page<ServeType> page = new Page<>(form.getPageNum() == null ? 1 : form.getPageNum(), form.getPageSize() == null ? 6 : form.getPageSize());
        QueryWrapper<ServeType> wrapper = new QueryWrapper<>();
        //判断类别名是否为空
        if (ObjectUtil.isNotEmpty(form.getServeTypeName())){
            wrapper.like("serve_type_name",form.getServeTypeName());
        }
        Page<ServeType> serveTypePage = serveTypeMapper.selectPage(page, wrapper);
        Page<ServeTypeDto> serveTypeDtoPage = PageDtoUtil.convertPage(serveTypePage, ServeTypeDto.class);
        return serveTypeDtoPage;
    }

    @Override
    public List<ServeType> getMyServeType() {
        List<ServeType> serveTypes = serveTypeMapper.selectList(null);
        return serveTypes;
    }

}
